package kr.hkit.powerjava.chapter22.work;

import java.util.ArrayList;
import java.util.StringTokenizer;

import javax.swing.JOptionPane;

public class TestMain {
	public static void main(String[] args) {
		ArrayList<Department> deptList= new ArrayList<>();
		DepartmentDAO deptDao= new  DepartmentDAO(deptList);
		deptDao.additem(new Department(1, "컴공", 3));
		deptDao.prnList();
		
		ArrayList<Student> stdList= new ArrayList<>();
		initList(stdList);//함수에있는 변수
		StudentManager sm= new StudentManager(stdList);
		int res=0;
		Student std=null;
		do {
			//System.out.println("메뉴를 선택하세요");
			try {//숫자로 바꿔라-Integer.parseInt
				res=Integer.parseInt(JOptionPane.showInputDialog("1.추가2.검색3.수정4.삭제5.리스트6.종료"));
				System.out.println(res);
			} catch (NumberFormatException e) {
				res=6;
			}
			
			switch (res) {
				case 1://추가
					std=inputStdInfo(true);
					sm.additem(std);
					break;
				case 2:
					std=sm.searchitem(inputStdInfo(false));
					if(std==null){
						System.out.println("해당학생 없음");
					}else{
						System.out.println(std);
						//JOptionPane.showMessageDialog(null, std);
					}
					break;
				case 3:
					std=inputStdInfo(true);
					sm.reitem(std);
					break;
				case 4:
					std=inputStdInfo(false);
					sm.delitem(std);
					break;
				case 5:
					sm.prnList();
					break;
				default:
					break;
			}
		} while (res<6);}
		private static Student inputStdInfo(boolean gubun){
			if(gubun){
				String std=JOptionPane.showInputDialog("학생번호 성명 국어 영어 수학");
				StringTokenizer st= new StringTokenizer(std);//공백을 기준으로 뽑아낸다
				int stdNum=Integer.parseInt(st.nextToken());
				String stdName=st.nextToken();
				int kor= Integer.parseInt(st.nextToken());
				int eng= Integer.parseInt(st.nextToken());
				int math= Integer.parseInt(st.nextToken());
				return new Student(stdNum, stdName, kor, eng, math);}
			else{
				String std=JOptionPane.showInputDialog("학생번호 성명");
				StringTokenizer st= new StringTokenizer(std);
				int stdNum=Integer.parseInt(st.nextToken());
				String stdName= st.nextToken();
				return new Student(stdNum, stdName);
			}
		}
		/*ArrayList<Student> stdList= new ArrayList<>();
		initList(stdList);
		prnHeader();
		prnList(stdList);
		//검색
		Student std= new Student(2, "홍길동");
		//System.out.println(stdList.indexOf(std)+"의 위치에 존재"+stdList.contains(std));
		System.out.println(stdList.lastIndexOf(std)+"의 위치에 존재"+stdList.contains(std));
		//총점순정렬
		Collections.sort(stdList);
		prnHeader();
		prnList(stdList);
		//수학점수별 정렬
		Comparator<Student> comp= new Comparator<Student>() {
			@Override
			public int compare(Student o1, Student o2) {
				return o1.getMathscore()-o2.getMathscore();
			}
		};
		Collections.sort(stdList, comp);
		prnHeader();
		prnList(stdList);*/
	/*	StudentManager sm= new StudentManager(stdList);
		sm.prnList();
		
		Student std=new Student(stdList.size()+1, "제시카", 90, 45, 55);
		sm.addStudent(std);
		sm.prnList();
		
		sm.delStudent(std);
		sm.prnList();
	}*/
	
	
	private static void prnHeader() {
		String header=String.format("%3s, %5s, %4s, %4s, %4s, %4s, %4.2s",
				"학번", "성명", "국어", "영어", "수학","총점","평균");
		System.out.println(header);
		
	}

	private static void prnList(ArrayList<Student> stdList) {
		for(Student std:stdList){
			System.out.println(std);
		}
		
	}

	private static void initList(ArrayList<Student> stdList) {
		stdList.add(new Student(1, "김태희", 87, 87, 87));
		stdList.add(new Student(2, "홍길동", 90, 87, 87));
		stdList.add(new Student(3, "마루치", 70, 70, 87));
		stdList.add(new Student(4, "아라치", 60, 65, 65));
		stdList.add(new Student(5, "철수", 50, 55, 50));
		stdList.add(new Student(6, "홍길동", 90, 87, 87));
		
	}
}
